﻿using FYSJcrm.Core.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FYSJcrm.Core.Web.Extensions.FilterExtensions
{
    public class CustomerActionFilter : IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
           
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var errorMsg = context.ModelState.Values.SelectMany(e => e.Errors).Select(e => e.ErrorMessage).FirstOrDefault();
                context.Result = new BadRequestObjectResult(
                    new AjaxResult { Code = HttpCode.BadRequest, ErrorMsg = string.IsNullOrWhiteSpace(errorMsg) ? "参数校验错误" : errorMsg }
                );
                return;
            }
        }
    }
}
